package com.gelsanalyzer.ui.swing.command;

import javax.swing.*;

public class ExitCommand extends JMenuItem implements Command {
        
    private final SaveCommand saveCommand;
    
    public ExitCommand(String label, SaveCommand saveCommand) {
            super(label);
            this.saveCommand = saveCommand;
        }        
        public void Execute() {
            // ask the user if they want to save their progress if it is not
            // already saved...
            int i = promptForSave();
            switch ( i ) {
                case JOptionPane.YES_OPTION:
                    saveCommand.Execute(); 
                    System.exit(0);
                    break;
                case JOptionPane.CANCEL_OPTION:
                    break;
                case JOptionPane.NO_OPTION:
                default:
                    System.exit(0);
                    break;
            }            
        }
        private int promptForSave() {
            int i = JOptionPane.showConfirmDialog(this.getParent(),
                "Would you like to save before exiting the application?",
                "Save before exit?",
                JOptionPane.YES_NO_CANCEL_OPTION);          
            return i;
        }
    
}
